//Hexamines
//(c) 2007 Felipe C.da S. Sanches <felipe.sanches@gmail.com>
// This is freesoftware under the terms of GNU General Public License version 3 or later
// See COPYING file for complete license terms.
//
//Board Object
Board = function(xmax,ymax,percentage){
	this.percentage=percentage;
	this.xmax=xmax;
	this.ymax=ymax;

	this.div = document.createElement("div");
	this.msg = document.createElement("div");

	//create the cell map
	this.map = new Array;
	for (var x=0;x<this.xmax;x++){
		var line = new Array();
		for (var y=0;y<this.ymax;y++){
			var c = new Cell(x,y);
			c.board = this;
			this.div.appendChild(c.div);
			line.push(c)
		}
		this.map.push(line);
	}
	this.link_neighbours();

	this.shadow_div = new Array;
	this.shadow_img = new Array;
	for(var i=0;i<6;i++){
		var image = document.createElement("img");
		var div = document.createElement("div");
		div.appendChild(image)
		div.style.position = "absolute";
		image.src = image_url(IMAGE_SHADOW);
		image.width = 0;
		image.height = 0;
		this.shadow_div.push(div);
		this.shadow_img.push(image);
		this.div.appendChild(div);
	}
	this.new_game();
	this.counter = new Counter(this, 2);
//	this.clock = new Clock(this);
}

Board.prototype.new_game = function(){
	this.game_is_over = false;
	this.multiclick = false;
	this.n_of_bombs=0;
	this.n_of_flags = 0;
	this.n_of_open_cells = 0;
	for (var x=0;x<this.xmax;x++){
		for (var y=0;y< this.ymax;y++){
			this.map[x][y].init();
			if (Math.random() < this.percentage){
				this.map[x][y].bomb=1;
				this.n_of_bombs++;
			} else {
				this.map[x][y].bomb=0;
			}
		}
	}
	var start = new Date();
	this.starttime = start.getTime(); 
}

Board.prototype.link_neighbours = function(){
	for (var x=0;x<this.xmax;x++){
		for (var y=0;y<this.ymax;y++){
			var c = this.map[x][y];
			if (x>0){
				c.n1 = this.map[x-1][y];
				if (y>0)
					c.n2 = this.map[x-1][y-1];
				if (y<ymax-1)
					c.n3 = this.map[x-1][y+1];
			}
			if (y>0)
				c.n4 = this.map[x][y-1];
			if (y<this.ymax-1)
				c.n5 = this.map[x][y+1];
			if (x<this.xmax-1)
				c.n6 = this.map[x+1][y];

			//odd lines:
			if (y%2){
				if (x<this.xmax-1){
					if (y>0)
						c.n2 = this.map[x+1][y-1];
					if (y<this.ymax-1)
						c.n3 = this.map[x+1][y+1];			
				} else {
					c.n2 = false;
					c.n3 = false;
				}
			}
		}
	}
}

Board.prototype.check_end_game = function(){
	if ((this.n_of_flags == this.n_of_bombs) && ((this.n_of_open_cells + this.n_of_bombs) == (this.xmax*this.ymax))){
		this.win_game();
	}
	return;
}

Board.prototype.win_game = function(){
	var end = new Date();
	var endtime = end.getTime();
	var totaltime = (endtime - this.starttime)/1000;

	if (saveScore){
		saveScore(totaltime);
	} else {
		new Message(this, "<b>Parabéns!</b> Você levou <b>" + totaltime + " segundos</b> para achar todas as bombas.<br/><br/><i>Clique aqui para jogar novamente.</i>", "restart");
	}
}

Board.prototype.gameover = function(){
	this.game_is_over = true;
	for (var x=0;x<this.xmax;x++){
		for (var y=0;y<this.ymax;y++){
			var c = this.map[x][y];
			if (c.bomb==0 && c.status == FLAG)
					c.image.src = image_url(IMAGE_WRONG_FLAG);
			if (c.bomb==1 && c.status != FLAG){
				c.image.src = image_url(IMAGE_BOMB);
			}
		}
	}
	new Message(this, "<b>BOOM!</b><br/><i>Clique aqui para tentar mais uma vez...</i>", "restart");
}

Board.prototype.restart = function(){
	this.b.div.removeChild(this);
	this.b.new_game();
}

//Helper Message Object:
Message = function(board, str, callback_str){
	new HTMLMessage(board, "<div style='size:12;text-align:center'>"+str+"</div>", callback_str);
}

//Helper HTMLMessage Object:
HTMLMessage = function(board, html, callback_str){
	board.msg.innerHTML = html;
	board.msg.style.backgroundImage = "url("+image_url("png/pixel.png")+")";
	board.msg.style.backgroundRepeat = "repeat";
	board.msg.style.border = "1px black solid";
	board.msg.style.position = "relative";
	board.msg.style.marginLeft = "50px";
	board.msg.style.marginRight = "50px";
	board.msg.style.padding = "10 10 10 10";
	board.div.appendChild(board.msg);
	board.msg.b = board;
	if (callback_str) board.msg.onclick = board[callback_str];
}

//Counter Object:

Counter = function(board, n, right){
	if (!board){
		// alert("You must pass a Board object to the Counter");
		return;
	}
	this.board=board;
	this.size=n;
	this.digits = new Array;
	for (var i=0;i<n;i++)
		this.digits[i] = document.createElement("img");

	this.set_digits(0);
	this.div = document.createElement("div");
	for (var i=0;i<this.size;i++)
		this.div.appendChild(this.digits[this.size-1-i]);
	this.div.style.position = "relative";
	this.div.style.top=-50;
	if (right) this.div.style.right=0;

	this.board.div.appendChild(this.div);
}


Counter.prototype.set_digits = function(value){
	for(var i=0;i<this.size;i++){
		this.digits[i].src = image_url(IMAGE_NUMBER_BASE + (value % 10) + ".png");
		value=Math.floor(value/10);
	}
}

//Clock Object:

Clock = function(board){
	this.counter = new Counter(board, 4, true);
	this.counter.set_digits()
	this.reset()	
}
Clock.prototype.reset = function(){
	this.time = 0;
	setInterval(function(){this.time++; this.counter.set_digits(this.time)}, 1000);
}


